//******************************************************************/
//版本说明:
//V0.1		2017-03-30	11:00	yshao	复制自SL909_G01_X32
//V2.1		2019-01-31	11:00	yshao	调整init_end和comm_en,init_mode
//******************************************************************/
//******************************************************************/
//			   全局参数定义
//******************************************************************/
//仿真模式
//`define	SIMULATION	1
//`timescale	1ps/1ps

//FPGA程序模式
`define		APP_MODE	1
//`define	TEST_MODE	2
//`define	BOOT_MODE	3

//程序功能模式
`define		SUB		0
//`define	ASSIST		1

//******************************************************************/
//			   FPGA顶层
//******************************************************************/
module SL909_G04(
		input	wire		sclkin,
		input   wire		key_in,
		input   wire		s_pullup,

		output  wire		JP202_PIN3,
		output  wire		JP202_PIN4,
		output	wire		JP202_PIN5,
		input	wire		JP202_PIN6,

		output	wire		sa_clk,
		output	wire	[2:0]	sa_cnt,
		output	wire		sa_dqm_l,
		output	wire		sa_dqm_h,
		output	wire	[10:0]	sa_addr,
		output	wire	[1:0]	sa_bank,
		inout	tri	[31:0]	sa_data,

		input	wire		mcu_dmx_tx,		//uart2_sync,
		output	wire		mcu_dmx_rx,		//uart3_sync,

		input	wire		mcu_fpga_ctrl,		//mcu到fpga的模式控制信号

		input	wire		mcu_spi_fpga,		//spi工作在通讯模式
		input	wire		mcu_spi_clk,
		input	wire		mcu_spi_mosi,
		output	wire		mcu_spi_miso,

		output	wire		spi2_cs,
		output	wire		spi2_clk,
		output	wire		spi2_mosi,
								
		output	wire		sd_clk,
		inout	tri		sd_cmd_sdi,
		input	wire		sd_dat0_sdo,
		input	wire		sd_dat1,
		input	wire		sd_dat2,
		inout	wire		sd_dat3_cs,
		input	wire		sd_cd,
		input	wire		sd_wp,

		input	wire		gp0_rxc,
		input	wire		gp0_rxdv,
		input	wire	[3:0]	gp0_rxd,
		output	wire		gp0_txc,
		output	wire		gp0_txen,
		output	wire	[3:0]	gp0_txd,
		
		input	wire		gp1_rxc,
		input	wire		gp1_rxdv,
		input	wire	[3:0]	gp1_rxd,
		output	wire		gp1_txc,
		output	wire		gp1_txen,
		output	wire	[3:0]	gp1_txd,

		output	wire	[7:0]	port_da,
		output	wire	[7:0]	port_le,
		inout	tri	[7:0]	port_ex,
		
		input	wire	[7:0]	port_in,
		
		output	wire		led_g
		);

//****************************************************************
//		内部信号
//****************************************************************
wire		resetb, oclk, sclk;
wire	[20:0]	time_us, sync_us;

//PHY设置总线相关信号
wire		init_mode, set_d_ok, ext_d_ok;
wire	[31:0]	set_addr;
wire	[7:0]	set_data, rd_data;

//MCU设置总线相关信号
wire		mcu_set_d_ok, mcu_ext_d_ok;
wire	[31:0]	mcu_set_addr;
wire	[7:0]	mcu_set_data, mcu_rd_data;
wire	[7:0]	mcu_rd_hub;

//显示数据相关
wire		vs, ds, h_start;
wire	[7:0]	data;
wire	[10:0]	h_num;
wire		l2048_mode, artnet_mode;

wire	[15:0]	state;
wire	[1:0]	color_restore_type;
wire	[7:0]	led_light, testmode;
wire	[63:0]	unit_color_adj;

wire		black;

//输出模块接口信号
wire		vs_out;
wire		disp_read_req;
wire	[23:0]	disp_read_addr;
wire	[3:0]	disp_read_cnt;
wire		disp_read_ack;
wire	[31:0]	disp_read_data;

//输出控制信号
wire		out_sync;
wire 	[7:0]	out_data;
wire 	[7:0]	out_data_n;
wire		clk_out;
wire		clk_out_n;

//DMX输出信号
wire	[1:0]	dmx_mode;
wire		dmx_send_flag;
wire	[7:0]	out_data_dmx;

//**********调试用信号**********
wire	[7:0]	sd_tout, hub_tout;
wire	[31:0]	a1_tout, out_tout, sa_tout;

//******************************************************************/
//			   参数定义
//******************************************************************/
//Flash程序区地址
parameter	INIT_ADDR	=14'b0000_0000_1100_00;//0x00C0_0000~0x00C3_FFFF
parameter	TEST_ADDR	=14'b0000_0000_1100_01;//0x00C4_0000~0x00C7_FFFF
parameter	ACT_ADDR	=14'b0000_0000_1100_10;//0x00C8_0000~0x00CB_FFFF

//程序版本信息
parameter	MAIN_FUNCTION	=  "S";		//ASCII "S"  
parameter	SUB_FUNCTION	=  "L";		//ASCII "L"  
parameter	MAIN_SOLUTION	=  9;		//"9"        
parameter	SUB_SOLUTION	=  9;		//"09"       
parameter	APPLICATION_TYPE=  "G";		//ASCII "G"  
parameter	MAIN_VERSION	=  8'd4;	//"03"       
parameter	SUB_VERSION	=  8'd2;	//"X01"  
parameter	MINI_VERSION	=  8'd3;	//" "  

//模块参数设置
defparam	comm_top.phy_comm.com_ctrl.INITIAL_ADDR		=INIT_ADDR;
defparam	comm_top.phy_comm.com_ctrl.TEST_ADDR		=TEST_ADDR;
defparam	comm_top.phy_comm.com_ctrl.ACTIVE_ADDR		=ACT_ADDR;

defparam	comm_top.phy_comm.state_ctrl.main_function	=MAIN_FUNCTION;
defparam	comm_top.phy_comm.state_ctrl.sub_function	=SUB_FUNCTION;
defparam	comm_top.phy_comm.state_ctrl.main_solution	=MAIN_SOLUTION;
defparam	comm_top.phy_comm.state_ctrl.sub_solution	=SUB_SOLUTION;
defparam	comm_top.phy_comm.state_ctrl.application_type	=APPLICATION_TYPE;
defparam	comm_top.phy_comm.state_ctrl.main_version	=MAIN_VERSION;
defparam	comm_top.phy_comm.state_ctrl.sub_version	=SUB_VERSION;
defparam	comm_top.phy_comm.state_ctrl.mini_version	=MINI_VERSION;

defparam	comm_top.mcu_comm.main_function		=	MAIN_FUNCTION;
defparam	comm_top.mcu_comm.sub_function		=	SUB_FUNCTION;
defparam	comm_top.mcu_comm.main_solution		=	MAIN_SOLUTION;
defparam	comm_top.mcu_comm.sub_solution		=	SUB_SOLUTION;
defparam	comm_top.mcu_comm.application_type	=	APPLICATION_TYPE;
defparam	comm_top.mcu_comm.main_version		=	MAIN_VERSION;
defparam	comm_top.mcu_comm.sub_version		=	SUB_VERSION;
defparam	comm_top.mcu_comm.mini_version		=	MINI_VERSION;

//**************************************************************
//			通讯模块
//**************************************************************
a1_comm_top comm_top(
		//时钟，按键，Led
		.sclkin(sclkin),
		.key_in(key_in),
		.led_g(led_g),
        
		//输出复位和时钟
		.resetb(resetb),
		.sclk(sclk),
		.oclk(oclk),
		.sa_clk(sa_clk),
		.clk_25M(clk_25M),

		//MCU通讯接口
		.mcu_fpga_ctrl(mcu_fpga_ctrl),
		
		.mcu_spi_fpga(mcu_spi_fpga),
		.mcu_spi_clk(mcu_spi_clk),
		.mcu_spi_mosi(mcu_spi_mosi),
		.mcu_spi_miso(mcu_spi_miso),

		.spi2_cs(spi2_cs),
		.spi2_clk(spi2_clk),
		.spi2_mosi(spi2_mosi),

		//PHY通讯接口
		.gp0_rxc(gp0_rxc),
		.gp0_rxdv(gp0_rxdv),
		.gp0_rxd(gp0_rxd),
		.gp0_txc(gp0_txc),
		.gp0_txen(gp0_txen),
		.gp0_txd(gp0_txd),

		.gp1_rxc(gp1_rxc),
		.gp1_rxdv(gp1_rxdv),
		.gp1_rxd(gp1_rxd),
		.gp1_txc(gp1_txc),
		.gp1_txen(gp1_txen),
		.gp1_txd(gp1_txd),

		//SD卡通讯接口
		.sd_clk(sd_clk),
		.sd_cmd_sdi(sd_cmd_sdi),
		.sd_dat0_sdo(sd_dat0_sdo),
		.sd_dat1(sd_dat1),
		.sd_dat2(sd_dat2),
		.sd_dat3_cs(sd_dat3_cs),
		.sd_cd(sd_cd),
		.sd_wp(sd_wp),

		//分频时钟
		.time_us(time_us),
		.sync_us(sync_us),

		//MCU总线接口
		.mcu_set_d_ok(mcu_set_d_ok),
		.mcu_ext_d_ok(mcu_ext_d_ok),
		.mcu_set_addr(mcu_set_addr),
		.mcu_set_data(mcu_set_data),
		.mcu_rd_data(mcu_rd_data),

		//PHY总线接口
		.init_mode(init_mode),
		.set_d_ok(set_d_ok),
		.ext_d_ok(ext_d_ok),
		.set_addr(set_addr),
		.set_data(set_data),
		.rd_data(rd_data),

		//显示数据接口
		.vs(vs),
		.ds(ds),
		.data(data),
		.h_start(h_start),
		.h_num(h_num),
		.l2048_mode(l2048_mode),
		.artnet_mode(artnet_mode),

		.state(state),
		.color_restore_type(color_restore_type),
		.led_light(led_light),
		.unit_color_adj(unit_color_adj),
		.testmode(testmode),

		.black(black),
		.identify_flag(),

		//背板控制接口
		.fpga_dmx_send(dmx_send_flag),
		.mcu_dmx_ten(mcu_dmx_ten),

		//SD卡播放指示
		.input_active(input_active),
		.sd_valid(sd_ok),

		//SD卡播放操作接口
	        .sd_play_flag(0),
		.sd_play_clk(0),
		.sd_play_cmd_out(0),
		.sd_play_cmd_oe(0),
		.sd_play_data(),
		//SD发送数据包接口
		.dis_send_flag(0),
		.dis_pre_flag(0),
		.dis_send_data(0),

		//调试接口
		.tout(a1_tout)
		);

assign	mcu_rd_data = mcu_rd_hub;

//**************************************************************
//         显示数据存储与读取
//************************************************************** 
s8_sdram_top_01 sdram_top(
		.resetb(resetb),
		.sclk(sclk),

		.init_mode(init_mode),
		.set_addr(set_addr),	
		.set_data(set_data),
		.set_d_ok(set_d_ok),
	
		.sync_16_us(sync_us[4]),
        
		.vsin(vs),
		.dsin(ds),
		.din(data),
		.h_start(h_start),
		.h_num(h_num),
		.l2048_mode(l2048_mode),
		.artnet_mode(artnet_mode),
        
		.state(state[7:0]),

		.vs_out(vs_out),
		.read_req (disp_read_req),
		.read_addr(disp_read_addr),
		.read_cnt(disp_read_cnt),
		.read_ack (disp_read_ack),
		.read_data(disp_read_data),

		.sa_clk(),
		.sa_cnt(sa_cnt),
		.sa_addr(sa_addr),
		.sa_bank(sa_bank),
		.sa_data(sa_data),
        
		.tout(sa_tout)
        	);

assign        sa_dqm_l=0;
assign        sa_dqm_h=0;

//**************************************************************
//		        输出控制
//************************************************************** 
//灯饰输出控制模块
out_ctrl_lamp out_ctrl_lamp(
		.resetb			(resetb),
		.sclk			(sclk),
		.oclk			(oclk),

		.set_d_ok		(set_d_ok),
		.set_addr		(set_addr[15:0]),
		.set_data		(set_data),

		.ext_d_ok		(ext_d_ok),
		.ext_addr		(set_addr[31:16]),

		.init_correct_d_ok	(0),
		.init_correct_addr	(0),
		.init_correct_data	(0),

		.t_us			(time_us[0]),
		.t_ms			(time_us[10]),
		.t_s			(time_us[20]),

		.out_en			(~init_mode),
		.v_start		(vs),
		.black			(1'b0),
		.state			(state[7:0]),
		.state_2		(state[15:8]),
		.color_restore_type	(color_restore_type),
		.led_light		(led_light),

		.unit_color_adj		({16'h8000,16'h8000,16'h8000,16'h8000}),//unit_color_adj),

		.vs_out			(vs_out),
		.disp_read_req		(disp_read_req),
		.disp_read_addr		(disp_read_addr),
		.disp_read_cnt		(disp_read_cnt),
		.disp_read_ack		(disp_read_ack),
		.disp_read_data		(disp_read_data),

		//串行输出信号
		.out_sync		(out_sync),
		.out_data		(out_data),
		.out_data_n		(out_data_n),
		.clk_out		(clk_out),
		.clk_out_n		(clk_out_n),

		//DMX输出信号
		.dmx_mode		(dmx_mode),
		.dmx_send_flag		(dmx_send_flag),
		.out_data_dmx		(out_data_dmx),

		//调试信号
		.tout			(out_tout)     
		);

//************************************************************/
//		背板接口
//************************************************************/
hub_port_map port_map(
		.resetb			(resetb),
		.sclk			(sclk),
		.s_pullup		(s_pullup),
        	
		//MCU设置接口
	        .cmd_d_ok		(mcu_ext_d_ok),
	        .cmd_addr		(mcu_set_addr),
	        .cmd_data		(mcu_set_data),
	        .cmd_rd_d		(mcu_rd_hub),
			
		//工作模式
		.testmode		(testmode),
        
		//MCU辅助通讯
		.mcu_dmx_ten		(mcu_dmx_ten),
		.mcu_dmx_tx		(mcu_dmx_tx),
		.mcu_dmx_rx		(mcu_dmx_rx),
        
		//串行输出信号
		.out_data		(out_data),
		.out_data_n		(out_data_n),
		.clk_out		(clk_out),
		.clk_out_n		(clk_out_n),
		
		//DMX输出信号
		.dmx_mode		(dmx_mode),
		.dmx_send_flag		(dmx_send_flag),
		.out_data_dmx		(out_data_dmx),
		
		//端口输入输出
		.port_da		(port_da),
		.port_le		(port_le),
		.port_ex		(port_ex),
        
		//调试信号
		.tout			(hub_tout)   
		);

//************************************************************/
//		调试接口
//************************************************************/                    
//assign  port_le = {sd_clk,sd_cmd_sdi,sd_dat0_sdo,sd_dat1,sd_dat2,sd_dat3_cs,sd_cd,sd_wp};  
//assign  port_da = {mcu_spi_fpga,mcu_spi_clk,mcu_spi_mosi,mcu_spi_miso,mcu_fpga_ctrl,mcu_sd_en,mcu_sd_rd,1'b0};  
//assign  port_le = {mcu_spi_fpga, mcu_spi_clk, mcu_spi_mosi, mcu_spi_miso, mcu_fpga_ctrl, mcu_sd_en, mcu_sd_rd};  
//assign  port_da = spi_tout;

//assign	JP202_PIN3 = mcu_spi_fpga;//sys_tout[1];//spi2_cs & mcu_spi_fpga;
//assign	JP202_PIN4 = time_us[15];//ttt_count[9];//spi2_clk | mcu_spi_clk;
//assign	JP202_PIN5 = a1_tout[0];//spi2_mosi | mcu_spi_mosi;

assign	JP202_PIN3 = mcu_spi_fpga;
assign	JP202_PIN4 = time_us[14];
assign	JP202_PIN5 = a1_tout[0];
endmodule


